﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Test;
using PdfLib;

namespace PdfLib.Test.PdfParser
{
    [ManualTestCase("Bookmark reader", "PDF parser")]
    public partial class TestCase_BookmarkReader : UserControl
    {
        public TestCase_BookmarkReader()
        {
            InitializeComponent();
        }

        private void btn_Open2_Click(object sender, EventArgs e)
        {
            this.OpenPage(this.txt_PdfPath.Text, (int)this.num_Page.Value);
        }

        private void btn_Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "PDF files(*.pdf)|*.pdf|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_PdfPath.Text = dlg.FileName;
                int pageCount = PdfPropertyReader.GetPageCount(this.txt_PdfPath.Text);
                this.num_Page.Value = 1;
                this.num_Page.Maximum = pageCount;

                this.OpenPage(this.txt_PdfPath.Text, (int)this.num_Page.Value);
            }
        }

        private void OpenPage(string pdfFilePath, int pageNum)
        {
            
            try
            {
                
                List<PdfBookmark> bookmarks = PdfBookmarkReader.GetAllBookmarks(pdfFilePath);
                this.DisplayBookmarks(bookmarks);
            }
            catch (Exception)
            {
            }
        }

        private void DisplayBookmarks(List<PdfBookmark> bookmarks)
        {
            this.tree_Bookmarks.Nodes.Clear();
            Dictionary<string, TreeNode> parentNodes = new Dictionary<string, TreeNode>();
            if (bookmarks != null && bookmarks.Count > 0)
            {
                bookmarks.Sort();
                foreach (PdfBookmark bookmark in bookmarks)
                {
                    TreeNode bookmarkTn = new TreeNode(bookmark.Title);
                    bookmarkTn.ImageIndex = 0;
                    bookmarkTn.SelectedImageIndex = 0;
                    bookmarkTn.Tag = bookmark;
                    if (string.IsNullOrEmpty(bookmark.Path))
                    {
                        this.tree_Bookmarks.Nodes.Add(bookmarkTn);
                    }
                    else
                    {
                        if (bookmark.Path.IndexOf(".") > 0)
                        {
                            string parentPath = bookmark.Path.Substring(0, bookmark.Path.LastIndexOf("."));
                            if (parentNodes.ContainsKey(parentPath))
                            {
                                TreeNode parentNode = parentNodes[parentPath];
                                parentNode.Nodes.Add(bookmarkTn);
                            }
                            else
                            {
                                this.tree_Bookmarks.Nodes.Add(bookmarkTn);
                            }
                        }
                        else
                        {
                            this.tree_Bookmarks.Nodes.Add(bookmarkTn);
                        }
                    }

                    parentNodes.Add(bookmark.Path, bookmarkTn);
                }
                this.tree_Bookmarks.ExpandAll();
            }
            this.tree_Bookmarks.Refresh();
        }

        private void tree_Bookmarks_MouseUp(object sender, MouseEventArgs e)
        {
            this.tree_Bookmarks.SelectedNode = this.tree_Bookmarks.GetNodeAt(e.X, e.Y);
            if(this.tree_Bookmarks.SelectedNode !=null)
            {
                PdfBookmark bookmark = (PdfBookmark) this.tree_Bookmarks.SelectedNode.Tag;
                this.txt_BookmarkTitle.Text = bookmark.Title;
                string[] pathNodes = bookmark.Path.Split(new char[] {'.'});
                for(int i=0;i<pathNodes.Length;i++)
                {
                    string pathNode = pathNodes[i];
                    int idx = int.Parse(pathNode) + 1;
                    switch(i)
                    {
                        case 0:
                            this.num_Path1.Value = idx;
                            break;
                        case 1:
                            this.num_Path2.Value = idx;
                            break;
                        case 2:
                            this.num_Path3.Value = idx;
                            break;
                        case 3:
                            this.num_Path4.Value = idx;
                            break;
                        default:
                            break;
                    }
                }

                string bookmarkContent = PdfBookmarkReader.ReadBookmarkContent(this.txt_PdfPath.Text, bookmark);
                this.rt_BookmarkContent.Text = bookmarkContent;
            }
        }

        #region bookmark locator
        private void num_Path1_ValueChanged(object sender, EventArgs e)
        {
            if(((int)this.num_Path1.Value)>0)
            {
                this.num_Path2.Enabled = true;
            }
            else
            {
                this.num_Path2.Enabled = false;
            }
        }

        private void num_Path2_ValueChanged(object sender, EventArgs e)
        {
            if (((int)this.num_Path2.Value) > 0)
            {
                this.num_Path3.Enabled = true;
            }
            else
            {
                this.num_Path3.Enabled = false;
            }
        }

        private void num_Path3_ValueChanged(object sender, EventArgs e)
        {
            if (((int)this.num_Path3.Value) > 0)
            {
                this.num_Path4.Enabled = true;
            }
            else
            {
                this.num_Path4.Enabled = false;
            }
        }

        private void rd_UseBookmarkPath_CheckedChanged(object sender, EventArgs e)
        {
            this.SetBookmarkLocatorType();
        }

        private void rd_UseBookmarkTitle_CheckedChanged(object sender, EventArgs e)
        {
            this.SetBookmarkLocatorType();
        }

        private void SetBookmarkLocatorType()
        {
            if (rd_UseBookmarkTitle.Checked)
            {
                this.txt_BookmarkTitle.Enabled = true;
                this.ck_UseRegex.Enabled = true;
                this.num_Path1.Enabled = false;
                this.num_Path2.Enabled = false;
                this.num_Path3.Enabled = false;
                this.num_Path4.Enabled = false;
            }
            else
            {
                this.txt_BookmarkTitle.Enabled = false;
                this.ck_UseRegex.Enabled = false;
                this.num_Path1.Enabled = true;
                this.num_Path2.Enabled = true;
                this.num_Path3.Enabled = true;
                this.num_Path4.Enabled = true;
            }
        }
        #endregion
    }
}
